package org.kfive.nova.io.bio;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 16-3-8
 * Time: 下午10:55
 * To change this template use File | Settings | File Templates.
 */
public class SocketClient {

    private static Logger LOG = Logger.getLogger(SocketClient.class);

    public static void run(String host,int port) throws IOException {
        LOG.info("Socket client start to connect SocketServer"+host+":"+port);
        Socket socket = new Socket(host,port);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
            String inputLine;
            Scanner sc=new Scanner(System.in);
            while (!"bye".equals(inputLine=sc.nextLine())){
                printWriter.write(inputLine);
                String ack = reader.readLine();
                LOG.info(ack);

            }


        }catch (IOException e){
            e.printStackTrace();
        }finally {
            socket.close();
        }
    }

    public static void main(String[] args) throws IOException {

        SocketClient.run("127.0.0.1",8080);
    }
}
